package com.MoveEat.activities.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.MoveEat.R;
import com.MoveEat.Utils.SimplePhotoCache;
import com.MoveEat.items.RestData;

public class RestDataAdapter extends ArrayAdapter<RestData>
{

	private ArrayList<RestData>	restData;
	int							resource;
	private SimplePhotoCache	photos;

	public RestDataAdapter(Context context, int textViewResourceId, ArrayList<RestData> items)
	{
		super(context, textViewResourceId, items);

		this.restData = items;
		this.resource = textViewResourceId;
		photos = new SimplePhotoCache(context.getResources().getDrawable(R.drawable.icon));
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{

		View restDataView;
		RestData rd = null;
		rd = restData.get(position);
		if (convertView == null)
		{
			restDataView = new LinearLayout(getContext());
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			restDataView = vi.inflate(R.layout.listitems, null);
		}
		else
		{
			restDataView = convertView;
		}
		// Get the text boxes from the listitem.xml file
		TextView restName = (TextView) restDataView.findViewById(R.id.txtRestName);
		TextView restAddress = (TextView) restDataView.findViewById(R.id.txtRestAddress);
		TextView restPhone = (TextView) restDataView.findViewById(R.id.txtRestPhone);
		TextView restPhone2 = (TextView) restDataView.findViewById(R.id.txtRestPhone2);
		ImageView icon = (ImageView) restDataView.findViewById(R.id.icon);
		icon.setTag(rd.getIconUrl());
		// Assign the appropriate data from our alert object above
		restName.setText(rd.getName());
		restAddress.setText(rd.getAddress());
		restPhone.setText(rd.getPhone());
		restPhone2.setText(rd.getPhone2());
		icon.setImageDrawable(null);
		if (rd.getIconUrl() != null && !TextUtils.isEmpty(rd.getIconUrl()) && rd.getIconUrl().contains("http"))
		{
			photos.loadPhoto(rd.getIconUrl(), icon);
		}

		restDataView.setTag(R.layout.listitems, rd.getId());

		return restDataView;
	}

	public void setNewList(ArrayList<RestData> restInfo)
	{
		restData = restInfo;

	}

}
